package com.joney.backstage.test.generator;

import org.junit.Test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <p>
 *     测试生成代码
 * </p>
 *
 * @author K神
 * @date 2017/12/18
 */
public class GeneratorServiceEntity {

    @Test
    public void generateCode(){
        String packageName = "com.joney.backstage";
        generateByTables(packageName, "T_USER");
    }

    private void generateByTables(String packageName, String... tableNames){
        GlobalConfig gc = new GlobalConfig();
        gc.setAuthor("joney");
        gc.setOutputDir("E:\\dog");
        gc.setFileOverride(true);
        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// 是否启用 二级缓存
        gc.setBaseResultMap(true);//是否生成 XML ResultMap
        gc.setBaseColumnList(true);//是否生成 XML columList

        DataSourceConfig db = new DataSourceConfig();
        db.setDbType(DbType.MYSQL);
        db.setUrl("jdbc:mysql://39.107.81.250:3306/test?useSSL=false");
        db.setDriverName("com.mysql.jdbc.Driver");
        db.setUsername("tiger");
        db.setPassword("Tiger-wxc!321");

        StrategyConfig sc = new StrategyConfig();
        sc.setTablePrefix("T_","");
        sc.setCapitalMode(true);
        sc.setEntityLombokModel(true);
        sc.setDbColumnUnderline(true);
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组


        new AutoGenerator()
                .setGlobalConfig(gc)
                .setDataSource(db)
                .setStrategy(sc)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }
}
